package leetcode_0002

// 链表节点
type ListNode struct {
	Val  int
	Next *ListNode
}

/**
 * 进一法
 *
 */
func addTwoNumbers1(l1 *ListNode, l2 *ListNode) *ListNode {

	head := &ListNode{0, nil}
	tail := head
	carry := 0

	for ; l1 != nil || l2 != nil; tail = tail.Next {
		sum := carry
		if l1 != nil {
			sum += l1.Val
			l1 = l1.Next
		}

		if l2 != nil {
			sum += l2.Val
			l2 = l2.Next
		}

		carry = sum / 10
		tail.Next = &ListNode{Val: sum % 10}
	}

	if carry > 0 {
		tail.Next = &ListNode{Val: carry}
	}

	return head.Next
}
